package com.zzyl.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.exception.BaseException;
import com.zzyl.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class WechatServiceImpl implements WechatService {


    /**
     * 获取openid
     *
     * @param code 登录凭证
     * @return 唯一标识
     * @throws IOException IO异常
     */
    // 微信三方调用接口
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";
    public JSONObject getOpenid(String code) throws IOException {
        Map<String, Object> requestParams = getAppConfig();
        requestParams.put("js_code", code);
        String result = HttpUtil.get(REQUEST_URL, requestParams);
        log.info("getOpenid result:{}", result);
        return JSONUtil.parseObj(result);
    }


    /**
     * 获取手机号
     */
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    @Override
    public String getPhone(String code) throws IOException {
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("code", code);
        String result = HttpUtil.post(PHONE_REQUEST_URL + getTokenUrl(), JSONUtil.toJsonStr(requestBody));
        log.info("getPhone result:{}", result);
        JSONObject entries = JSONUtil.parseObj(result);
        Assert.isTrue(!entries.getInt("errcode").equals(0), entries.getStr("errmsg"));
        return entries.getJSONObject("phone_info").getStr("purePhoneNumber");
    }


    /**
     * 获取接口调用凭据
     */
    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    public String getTokenUrl() throws IOException {
        Map<String, Object> requestParams = getAppConfig();
        String result = HttpUtil.get(TOKEN_URL, requestParams);
        log.info("getToken result:{}", result);
        JSONObject entries = JSONUtil.parseObj(result);
        Assert.isTrue(!ObjectUtil.isNotEmpty(entries.getInt("errcode")), entries.getStr("errmsg"));
        return entries.getStr("access_token");
    }


    /**
     * 获取小程序应用配置
     * @return 参数集合
     */
    // 从配置文件读取小程序appId
    @Value("${zzyl.wechat.appId}")
    private String appid;
    // 从配置文件读取小程序appSecret
    @Value("${zzyl.wechat.appSecret}")
    private String secret;
    public Map<String, Object> getAppConfig() {
        Map<String, Object> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appid", appid);
        requestUrlParam.put("secret", secret);
        return requestUrlParam;
    }





}
